package com.zte.market.service.impl;

import com.zte.login.service.UserService;
import com.zte.market.mapper.OrderMapper;
import com.zte.market.pojo.Custom;
import com.zte.market.pojo.Order;
import com.zte.market.pojo.otherPojo.DateTemp;
import com.zte.market.service.CustomService;
import com.zte.market.service.OrderService;
import com.zte.market.service.OrderdetailsService;
import com.zte.market.service.otherService.CkWarehouseService;
import com.zte.util.PageModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 许月
 * @Date: 2022/10/05/19:21
 */
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderdetailsService orderdetailsService;//订单详情 service 对象

    @Autowired
    private UserService userService;  //用户 service 对象

    @Autowired
    private CkWarehouseService ckWarehouseService;  //出库 service 对象

    @Autowired
    private CustomService customService;  // 客户 service 对象



    /**
     * 获取所有订单
     * @return
     */
    @Override
    public List<Order> getAll() {
        List<Order> orderList =new ArrayList<>();
        orderList = orderMapper.queryAll();
        for (Order order : orderList) {
            //设置客户信息
            order.setCustom(customService.queryByCustomId(order.getCustomid()));
            //设置 操作人信息
            order.setOperatorUser(userService.getUserById(order.getOperatorid()));
            //设置 审核人信息
            order.setCheckUser(userService.getUserById(order.getCheckid()));
            //设置 出库信息
            order.setCkWarehouse(ckWarehouseService.getByWarehouseId(order.getWarehouseId()));
            //设置 订单详情列表
            order.setOrderdetailsList(orderdetailsService.queryByOrderId(order.getOrderid()));
        }
        return orderList;
    }


    /**
     * 根据操作人id获取订单
     * @param operatorId
     * @return
     */
    @Override
    public List<Order> getAllByOperatorId(Integer operatorId) {
        List<Order> orderList =new ArrayList<>();
        orderList = orderMapper.getAllByOperatorId(operatorId);
        for (Order order : orderList) {
            //设置客户信息
            order.setCustom(customService.queryByCustomId(order.getCustomid()));
            //设置 操作人信息
            order.setOperatorUser(userService.getUserById(order.getOperatorid()));
            //设置 审核人信息
            order.setCheckUser(userService.getUserById(order.getCheckid()));
            //设置 出库信息
            order.setCkWarehouse(ckWarehouseService.getByWarehouseId(order.getWarehouseId()));
            //设置 订单详情列表
            order.setOrderdetailsList(orderdetailsService.queryByOrderId(order.getOrderid()));
        }
        return null;
    }


    /**
     * 通过点击查询按钮获取的参数进行的订单查询
     * @param orderId 订单编号（模糊查询）
     * @param customIdList 客户id列表（通过模糊查询客户姓名得到客户id列表）
     * @param startDate 订购的最早时间
     * @param endDate 订购的最后时间
     * @param minPrice 最低的金额
     * @param maxPrice 最高的金额
     * @param status 状态（“未审核”，“审核中”，”审核通过“，”审核不通过“）
     * @return
     */
    @Override
    public List<Order> getAllByParams(String orderId , List<Integer> customIdList, DateTemp dateTemp, Double minPrice, Double maxPrice, String status) {
        List<Order> orderList = orderMapper.queryAllByParams(orderId, customIdList, dateTemp, minPrice, maxPrice, status);
        for (Order order : orderList) {
            //设置客户信息
            order.setCustom(customService.queryByCustomId(order.getCustomid()));
            //设置 操作人信息
            order.setOperatorUser(userService.getUserById(order.getOperatorid()));
            //设置 审核人信息
            order.setCheckUser(userService.getUserById(order.getCheckid()));
            //设置 出库信息
            order.setCkWarehouse(ckWarehouseService.getByWarehouseId(order.getWarehouseId()));
            //设置 订单详情列表
            order.setOrderdetailsList(orderdetailsService.queryByOrderId(order.getOrderid()));
        }
        return orderList;
    }


    /**
     * 通过订单编号获取 订单信息（用于订单的 查看详情和修改）
     * @param orderId
     * @return
     */
    @Override
    public Order getByOrderId(String orderId) {
        Order order = orderMapper.queryById(orderId);
        //设置客户信息
        order.setCustom(customService.queryByCustomId(order.getCustomid()));
        //设置 操作人信息
        order.setOperatorUser(userService.getUserById(order.getOperatorid()));
        //设置 审核人信息
        order.setCheckUser(userService.getUserById(order.getCheckid()));
        //设置 出库信息
        order.setCkWarehouse(ckWarehouseService.getByWarehouseId(order.getWarehouseId()));
        //设置 订单详情列表
        order.setOrderdetailsList(orderdetailsService.queryByOrderId(order.getOrderid()));
        return order;
    }

    /**
     * 将数据进行分页
     * @param orderList 查询总记录数
     * @param pageSize 页面尺寸
     * @param pageNum 当前页面号
     * @return
     */
    @Override
    public PageModel<Order> queryByPage(List<Order> orderList, int pageSize, int pageNum) {

        PageModel<Order> orderPage = new PageModel<>();
        if(orderList != null && orderList.size()!=0){
            orderPage.setPageNum(pageNum);//设置当前页
            orderPage.setPageSize(pageSize);//设置当前页的尺寸
            //总页数
            int totalPage = orderList.size()/pageSize;
            if(orderList.size()%pageSize != 0){
                totalPage += 1;
            }
            orderPage.setTotalPage(totalPage); //设置总页数
            orderPage.setTotalRows(orderList.size());//设置总记录数

            List<Order> pageList = new ArrayList<>();
            int start = (pageNum-1)*pageSize;
            int end = pageNum*pageSize;//当页数据中最后一条数据对应的索引
            if(orderList.size()<end){
                end = orderList.size();
            }
            for(int i=start;i<end;i++){
                pageList.add(orderList.get(i));
            }

            orderPage.setData(pageList);//设置存储数据
        }else {
            orderPage.setTotalPage(1);
            orderPage.setPageNum(1);
            orderPage.setPageSize(7);
            orderPage.setTotalRows(0);
            orderPage.setData(null);
        }

        return orderPage;
    }

    /**
     * 根据 订单编号删除 订单
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteByOrderid(String orderId) {
        Integer delete = orderMapper.deleteByOrderid(orderId);
        return delete;
    }


    /**
     * 添加订单操作
     * @param order
     * @return
     */
    @Override
    public Integer addByOrder(Order order) {
        Integer rows = orderMapper.addByOrder(order);
        return rows;
    }

    /**
     * 修改订单操作
     * @param order
     * @return
     */
    @Override
    public Integer updateByOrder(Order order) {
        if(order != null){
            Integer update = orderMapper.updateByOrder(order);
            return update;
        }
        return null;
    }

    /**
     * 通过 审核人id查询订单
     * @param uid
     * @return
     */
    @Override
    public List<Order> getAllBycheckid(Integer uid) {
        List<Order> orderList = orderMapper.queryAllBycheckid(uid);
        for (Order order : orderList) {
            //设置客户信息
            order.setCustom(customService.queryByCustomId(order.getCustomid()));
            //设置 操作人信息
            order.setOperatorUser(userService.getUserById(order.getOperatorid()));
            //设置 审核人信息
            order.setCheckUser(userService.getUserById(order.getCheckid()));
            //设置 出库信息
            order.setCkWarehouse(ckWarehouseService.getByWarehouseId(order.getWarehouseId()));
            //设置 订单详情列表
            order.setOrderdetailsList(orderdetailsService.queryByOrderId(order.getOrderid()));
        }
        return orderList;
    }
}
